package com.wzh.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.mianshiya.common.ErrorCode;
import com.wzh.mianshiya.exception.BusinessException;
import com.wzh.mianshiya.mapper.QuestionBankThumbMapper;
import com.wzh.mianshiya.model.entity.Post;
import com.wzh.mianshiya.model.entity.QuestionBankThumb;
import com.wzh.mianshiya.model.entity.User;
import com.wzh.mianshiya.service.PostService;
import com.wzh.mianshiya.service.QuestionBankThumbService;
import javax.annotation.Resource;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 帖子点赞服务实现
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@Service
public class QuestionBankThumbServiceImpl extends ServiceImpl<QuestionBankThumbMapper, QuestionBankThumb>
        implements QuestionBankThumbService {

    @Resource
    private PostService postService;

    /**
     * 点赞
     *
     * @param postId
     * @param loginUser
     * @return
     */
    @Override
    public int doQuestionBankThumb(long postId, User loginUser) {
        // 判断实体是否存在，根据类别获取实体
        Post post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        QuestionBankThumbService QuestionBankThumbService = (QuestionBankThumbService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return QuestionBankThumbService.doQuestionBankThumbInner(userId, postId);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doQuestionBankThumbInner(long userId, long postId) {
        QuestionBankThumb QuestionBankThumb = new QuestionBankThumb();
        QuestionBankThumb.setUserId(userId);
        QuestionBankThumb.setPostId(postId);
        QueryWrapper<QuestionBankThumb> thumbQueryWrapper = new QueryWrapper<>(QuestionBankThumb);
        QuestionBankThumb oldQuestionBankThumb = this.getOne(thumbQueryWrapper);
        boolean result;
        // 已点赞
        if (oldQuestionBankThumb != null) {
            result = this.remove(thumbQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = postService.update()
                        .eq("id", postId)
                        .gt("thumbNum", 0)
                        .setSql("thumbNum = thumbNum - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(QuestionBankThumb);
            if (result) {
                // 点赞数 + 1
                result = postService.update()
                        .eq("id", postId)
                        .setSql("thumbNum = thumbNum + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

}




